﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DNA.Mvc.Services;
using DNA.OpenAPI.Sitemap;
using System.Web;
using System.IO;

namespace DNA.Mvc.Publishing.Services
{
    public class ArchiveSiteMapGenerator : SiteMapGeneratorBase
    {
        IPubService srv;
        IDataContext context;

        public ArchiveSiteMapGenerator(IDataContext dbContext, IPubService service)
        {
            srv = service;
            context = dbContext;
        }

        protected override void OnGenerateSiteMap(List<OpenAPI.Sitemap.Sitemap> sitemaps)
        {
            var webs = context.Webs.Filter(w => w.Name.Equals("home") || w.Type.Equals((int)WebTypes.Team) || w.Type.Equals((int)WebTypes.Public));
            foreach (var web in webs)
                CreateSiteMap(web, sitemaps);
        }

        private void CreateSiteMap(Web web, List<Sitemap> sitemaps)
        {
            var archives = srv.GetArchives(web.Name);
            var rootPath = UrlResolver.Resolve(new HttpContextWrapper(HttpContext.Current));

            //.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority;
            foreach (var archive in archives)
            {
                string sitemapPath = HttpContext.Current.Server.MapPath("~/shared/public/sitemaps/");
                //_pageSitemap.Location = context.ApplicationPath + "/webshared/home/sitemaps/" + web.Name + ".xml";

                var archiveFileName = "archive-"+web.Name+"-" + archive.Year.ToString() + "-" + archive.Month.ToString() + ".xml";
                var archiveFile = sitemapPath + archiveFileName;
                var _archiveSitemap = new Sitemap();
                _archiveSitemap.LastModified = new FileInfo(archiveFile).CreationTime.ToString("yyyy-MM-dd");

                if ((archive.Year == DateTime.Now.Year) && (archive.Month == DateTime.Now.Month) && System.IO.File.Exists(archiveFile))
                    System.IO.File.Delete(archiveFile);

                if (!System.IO.File.Exists(archiveFile))
                {
                    _archiveSitemap.LastModified = DateTime.Now.ToString("yyyy-MM-dd");
                    var articles = srv.GetArchivePosts(web.Name, archive.Year, archive.Month);

                    var _arcivheUrlset = new SitemapUrlset() { Urls = new List<SitemapUrl>() };
                    foreach (var art in articles)
                    {
                        var article = new ArticleWrapper(art);
                        if ((article.IsPrivate) || (!article.IsAppoved) || (!article.IsPublished)) continue;

                        _arcivheUrlset.Urls.Add(new SitemapUrl()
                        {
                            Url = article.PermaLink,
                            LastModified = article.LastModified.ToString("yyyy-MM-dd"),
                            ChangeFrequently = SitemapChangeFrequently.Monthly,
                            Priority = 0.5M
                        });
                    }
                    DNA.Utility.XmlSerializerUtility.SerializeToXmlFile(archiveFile, typeof(SitemapUrlset), _arcivheUrlset);
                }

                _archiveSitemap.Location = rootPath + "/webshared/home/sitemaps/" + archiveFileName;
                //_sitemapIndex.SiteMapFiles.Add(_archiveSitemap);
                sitemaps.Add(_archiveSitemap);
            }
        }
    }
}
